﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace EventLogAdmin
{
    public partial class EventLogEdit : Form
    {
        private bool addingLog = false;
        private EventLogProperties eventLogProperties;

        public EventLogEdit()
        {
            InitializeComponent();
        }

        public DialogResult AddEventlog(string machineName)
        {
            Text = "Event Log New";
            txtComputer.Text = machineName;
            addingLog = true;
            txtName.ReadOnly = false;
            return ShowDialog();
        }
        public DialogResult EditEventLog(string machineName, string eventLogName)
        {
            Text = "Event Log Edit";
            txtComputer.Text = machineName;
            eventLogProperties = EventLogUtil.GetEventLogProperties(machineName, eventLogName);
            txtName.Text = eventLogProperties.Name;
            numericUpDownMaxSize.Value = eventLogProperties.MaxSize / 1024;
            radioButtonNoOverwrite.Checked = true;
            radioButtonOverwriteAsNeeded.Checked = eventLogProperties.Retention == 0;
            radioButtonArchive.Checked = eventLogProperties.Retention == -1 && eventLogProperties.AutoBackupLogFiles == 1 ;

            return ShowDialog();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length == 0)
            {
                MessageBox.Show("You must specify a name for the Event Log!", "Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtName.Focus();
            }
            else
            {
                try
                {
                    if (addingLog)
                    {
                        if (addingLog && EventLog.SourceExists(txtName.Text))
                        {
                            MessageBox.Show("An Event log or source with the specified name already exists!", "Exists", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            txtName.Focus();
                            return;
                        }
                        EventLog.CreateEventSource(txtName.Text, txtName.Text, txtComputer.Text);
                        eventLogProperties = new EventLogProperties() { MachineName = txtComputer.Text, Name = txtName.Text };
                    }
                    eventLogProperties.MaxSize = (long)numericUpDownMaxSize.Value * 1024;
                    eventLogProperties.Retention = radioButtonOverwriteAsNeeded.Checked ? 0 : -1;
                    eventLogProperties.AutoBackupLogFiles = radioButtonArchive.Checked ? 1 : 0;
                    EventLogUtil.SetEventLogProperties(eventLogProperties);
                    if (eventLogProperties.Name != "Security" && (txtComputer.Text == "." || System.Net.Dns.GetHostName().ToUpper() == txtComputer.Text.ToUpper()))
                        EventLog.WriteEntry(eventLogProperties.Name, string.Format("Event Log properties changed.\r\n{0}", eventLogProperties));
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                    Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Saving", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }
    }
}
